package com.lxh.lxhcrm.common;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConvertor implements Converter<String, Date> {
    @Override
    public Date convert(String s) {
        SimpleDateFormat sdf = null;
        //判断string类型的日期，是哪种格式
        if(s.matches("\\d{4}-\\d{2}-\\d{2}")){
            sdf =  new SimpleDateFormat("yyyy-MM-dd");
        }else if(s.matches("\\d{4}/\\d{2}/\\d{2}")){
            sdf =  new SimpleDateFormat("yyyy/MM/dd");
        }else if(s.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")){
            sdf =  new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        }
        try {
            return sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

